/*
 * www.qwfys.com Inc.
 * Copyright (c) 2004- 2020 All Rights Reserved.
 */

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.format.Formatter;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @author lwk
 * @version MessageAo, v 0.1 6/4/20 4:03 PM lwk
 */
@Configuration
public class MvcConfig implements Serializable {

    // 时间格式化
    private static final DateTimeFormatter LOCAL_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
    private static final DateTimeFormatter LOCAL_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    @Bean
    public Formatter<LocalDate> localDateFormatter() {
        return new Formatter<LocalDate>() {
            @Override
            public LocalDate parse(String text, Locale locale) throws ParseException {
                return LocalDate.parse(text, LOCAL_DATE_FORMATTER);
            }
            @Override
            public String print(LocalDate object, Locale locale) {
                return object.format(LOCAL_DATE_FORMATTER);
            }
        };
    }
    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter() {
        return new Formatter<LocalDateTime>() {
            @Override
            public LocalDateTime parse(String text, Locale locale) throws ParseException {
                return LocalDateTime.parse(text, LOCAL_DATE_TIME_FORMATTER);
            }
            @Override
            public String print(LocalDateTime object, Locale locale) {
                return object.format(LOCAL_DATE_TIME_FORMATTER);
            }
        };
    }
}
